﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterpreterClass
{

    public class Context
    {
        public readonly string AnalyStr;
        internal int analySumValue;

        public int AnalySumValue
        {
            get { return analySumValue; }
        }
        internal string subAnalyStr;

        public Context(string str)
        {
            this.AnalyStr = str;
            this.subAnalyStr = str;
        }
    }

    public abstract class Expression
    {
        public static Dictionary<string, int> DataDic = new Dictionary<string, int>();
        static Expression()
        {
            string str1 = "一二三四五六七八九";
            for (int i = 0; i < str1.Length; i++)
            {
                DataDic.Add(str1[i].ToString(), i + 1);
            }
        }

        public virtual void ConverExpression(Context context)
        {
            if (context.subAnalyStr.Length <= 0)
            {
                return;
            }
            foreach (var kvp in DataDic)
            {
                if (context.subAnalyStr.EndsWith(kvp.Key + GetSuffix()) || context.subAnalyStr.EndsWith("万"))
                {
                    context.analySumValue += GetCurVal(kvp.Value, ref context.subAnalyStr);
                    context.subAnalyStr = context.subAnalyStr.Substring(0, context.subAnalyStr.Length - GetLength());
                    break;
                }
                else if (context.subAnalyStr.EndsWith("零"))
                {
                    context.subAnalyStr = context.subAnalyStr.Substring(0, context.subAnalyStr.Length - 1);
                    break;
                }
                else if (kvp.Key == "九" && context.subAnalyStr.EndsWith("十")&& context.subAnalyStr.Length==1)
                {
                    context.analySumValue += 10;
                    context.subAnalyStr = context.subAnalyStr.Substring(0, context.subAnalyStr.Length - 1);
                    break;
                }
            }
        }

        public abstract string GetSuffix();
        public abstract int GetMultiple();

        public virtual int GetCurVal(int key, ref string str)
        {
            return key * GetMultiple();
        }
        public virtual int GetLength() { return 2; }
    }

    public class GeExpression : Expression
    {
        public override int GetLength()
        {
            return 1;
        }

        public override int GetMultiple()
        {
            return 1;
        }

        public override string GetSuffix()
        {
            return "";
        }
    }

    public class ShiExpression : Expression
    {

        public override int GetMultiple()
        {
            return 10;
        }

        public override string GetSuffix()
        {
            return "十";
        }
    }

    public class BaiExpression : Expression
    {
        public override int GetMultiple()
        {
            return 100;
        }

        public override string GetSuffix()
        {
            return "百";
        }
    }

    public class QianExpression : Expression
    {
        public override int GetMultiple()
        {
            return 1000;
        }

        public override string GetSuffix()
        {
            return "千";
        }
    }

    public class WanExpression : Expression
    {
        /// <summary>
        /// 用于解析万位的数据
        /// </summary>
        private static List<Expression> expresLs = new List<Expression>() { new GeExpression(), new ShiExpression(), new BaiExpression(), new QianExpression() };

        private string wanStr = string.Empty;

        private int wanLength = 0;

        public override int GetCurVal(int key, ref string str)
        {
            int starPos = 0;
            //1. 截取万位上的数据
            if (str.Contains("亿"))
            {
                starPos = str.LastIndexOf("亿");
            }
            else if (str.Contains("兆"))
            {
                starPos = str.LastIndexOf("兆");
            }
            else if (str.Contains("京"))
            {
                starPos = str.LastIndexOf("京");
            }
            wanStr = str.Substring(starPos, str.Length - starPos - 1);
            wanLength = wanStr.Length + 1;
            Context con = new Context(wanStr);
            expresLs.ForEach(ret => { ret.ConverExpression(con); });
            return con.AnalySumValue * GetMultiple();
        }

        public override int GetMultiple()
        {
            return 10000;
        }

        public override string GetSuffix()
        {
            return "万";
        }

        public override int GetLength()
        {
            return this.wanLength;
        }
    }
}
